/**
 * Carnegie Mellon University
 * School of Computer Science
 * MSIT eBusiness Technology
 * 
 * Team 8 - Infinity (2011 Spring)
 * infinity-team8@googlegroups.com
 * 
 * Professor: Jeffrey L. Eppinger
 * Program Faculty: Sujata Telang
 */

package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.EmployeeDAO;
import model.FundDAO;
import model.Model;

import org.mybeans.dao.DAOException;

import databean.EmployeeBean;
import databean.FundBean;


public class EmployeeViewFundsAction extends Action {

	private FundDAO     fundDAO;
	private EmployeeDAO employeeDAO;

	public EmployeeViewFundsAction(Model model) {
		fundDAO = model.getFundDAO();
		employeeDAO = model.getEmployeeDAO();
	}

	public String getName() { return "employee-view-funds.do"; }

	public String perform(HttpServletRequest request,HttpServletResponse response) {
        // Set up the errors list
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
        //returns the existing session or null, if the session does not exist.
        HttpSession session = request.getSession(false);
        
		try {
	        
	        request.setAttribute("employeeList",employeeDAO.getEmployees());
			request.setAttribute("employee",(EmployeeBean) request.getSession(false).getAttribute("employee"));
	        
	        
	        FundBean[] newFundList = fundDAO.getFunds();
	        session.setAttribute("fundList",newFundList);
	
	        // After successful registration (and login) send to...
	        String redirectTo = (String) session.getAttribute("redirectTo");
	        if (redirectTo != null) return redirectTo;

	        return "employee-view-funds.jsp";
        } catch (DAOException e) {
        	errors.add(e.getMessage());
        	return "employee-error.jsp";
        }
    }
}
